package com.tool_stdy.utils.guava.ratelimit;

import com.google.common.util.concurrent.RateLimiter;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @auther: cc.ly
 * @date: 2019/1/29/029
 */

public class RatelimitTestRun {
    static ExecutorService executorService = Executors.newFixedThreadPool(2);
    private static final RateLimiter rateLimiter = RateLimiter.create(5);
    static long begin = System.currentTimeMillis() / 1000;

    public static void main(String[] args) {
        for (int i = 0; i < 30; i++) {
            executorService.submit(RatelimitTestRun::testRabteLimit);
            //  testRabteLimit();
        }
    }

    public static void testRabteLimit() {
        double acquire = rateLimiter.acquire();
        System.out.println((System.currentTimeMillis() / 1000 - begin) + "---" + Thread.currentThread().getName() + "===" + acquire);
    }
}
